package com.jeecg.mineattribute.controller;

import com.jeecg.mineattribute.entity.MineAttributeEntity;
import com.jeecg.mineattribute.service.MineAttributeServiceI;
import com.pub.dbtools.DataSimpDto;
import com.pub.dbtools.StaticTools;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.exception.BusinessException;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.common.TreeChildCount;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.core.util.MyBeanUtils;

import java.io.OutputStream;

import org.jeecgframework.core.util.BrowserUtils;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.entity.TemplateExportParams;
import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.vo.TemplateExcelConstants;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.jeecgframework.core.util.ResourceUtil;

import java.io.IOException;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.util.Map;
import java.util.HashMap;

import org.jeecgframework.core.util.ExceptionUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.jeecgframework.core.beanvalidator.BeanValidators;

import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import java.math.BigDecimal;
import java.net.URI;

import org.springframework.http.MediaType;
import org.springframework.web.util.UriComponentsBuilder;

/**
 * @Title: Controller
 * @Description: 矿井信息维护
 * @author onlineGenerator
 * @date 2017-11-13 17:22:40
 * @version V1.0
 *
 */
@Controller
@RequestMapping("/mineAttributeController")
public class MineAttributeController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger
			.getLogger(MineAttributeController.class);

	@Autowired
	private MineAttributeServiceI mineAttributeService;
	@Autowired
	private SystemService systemService;
	@Autowired
	private Validator validator;

	/**
	 * 矿井信息维护列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "Tlist")
	public ModelAndView list(MineAttributeEntity mineAttribute,
			HttpServletRequest request) {
		// 获取当前登录用户
		String fd_lrr = ResourceUtil.getUserSystemData("sysUserCode");
		if (fd_lrr.equals("admin")) {
			if (StringUtil.isNotEmpty(mineAttribute.getId())) {
				mineAttribute = mineAttributeService.getEntity(
						MineAttributeEntity.class, mineAttribute.getId());
				request.setAttribute("mineAttributePage", mineAttribute);
			}
			return new ModelAndView("com/jeecg/mineattribute/mineAttributeList");
		} else {
			switch (Integer.parseInt(fd_lrr)) {
			case 18:
				fd_lrr = "1890煤矿";
				break;
			case 19:
				fd_lrr = "1930煤矿";
				break;
			case 21:
				fd_lrr = "2130煤矿";
				break;

			}
			String sql = "select id from mine_attribute where mine_type='"
					+ fd_lrr + "'";
			DataSimpDto getId = new DataSimpDto(sql);
			String id = getId.getDataValue("id", 0);
			if (StringUtil.isNotEmpty(id)) {
				mineAttribute = mineAttributeService.getEntity(
						MineAttributeEntity.class, id);
				request.setAttribute("mineAttributePage", mineAttribute);
			}
			return new ModelAndView(
					"com/jeecg/mineattribute/mineAttributeTList");
		}
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 * @param dataGrid
	 * @param user
	 */

	@RequestMapping(params = "datagrid")
	public void datagrid(MineAttributeEntity mineAttribute,
			HttpServletRequest request, HttpServletResponse response,
			DataGrid dataGrid) {
		CriteriaQuery cq = new CriteriaQuery(MineAttributeEntity.class,
				dataGrid);
		// 查询条件组装器
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq,
				mineAttribute, request.getParameterMap());
		try {
			// 自定义追加查询条件
		} catch (Exception e) {
			throw new BusinessException(e.getMessage());
		}
		cq.add();
		this.mineAttributeService.getDataGridReturn(cq, true);
		TagUtil.datagrid(response, dataGrid);
	}

	/**
	 * 删除矿井信息维护
	 * 
	 * @return
	 */
	@RequestMapping(params = "doDel")
	@ResponseBody
	public AjaxJson doDel(MineAttributeEntity mineAttribute,
			HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		mineAttribute = systemService.getEntity(MineAttributeEntity.class,
				mineAttribute.getId());
		message = "矿井信息维护删除成功";
		try {
			mineAttributeService.delete(mineAttribute);
			systemService.addLog(message, Globals.Log_Type_DEL,
					Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "矿井信息维护删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 批量删除矿井信息维护
	 * 
	 * @return
	 */
	@RequestMapping(params = "doBatchDel")
	@ResponseBody
	public AjaxJson doBatchDel(String ids, HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		message = "矿井信息维护删除成功";
		try {
			for (String id : ids.split(",")) {
				MineAttributeEntity mineAttribute = systemService.getEntity(
						MineAttributeEntity.class, id);
				mineAttributeService.delete(mineAttribute);
				systemService.addLog(message, Globals.Log_Type_DEL,
						Globals.Log_Leavel_INFO);
			}
		} catch (Exception e) {
			e.printStackTrace();
			message = "矿井信息维护删除失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 添加矿井信息维护
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doAdd")
	@ResponseBody
	public AjaxJson doAdd(MineAttributeEntity mineAttribute,
			HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		message = "矿井信息维护添加成功";
		try {
			mineAttributeService.save(mineAttribute);
			systemService.addLog(message, Globals.Log_Type_INSERT,
					Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "矿井信息维护添加失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 更新矿井信息维护
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doUpdate")
	@ResponseBody
	public AjaxJson doUpdate(MineAttributeEntity mineAttribute,
			HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		message = "矿井信息维护更新成功";
		
		MineAttributeEntity t = mineAttributeService.get(
				MineAttributeEntity.class, mineAttribute.getId());
		String fd_lrr=t.getMineType();//获取矿类型
		try {
			MyBeanUtils.copyBeanNotNull2Bean(mineAttribute, t);
			mineAttributeService.saveOrUpdate(t);
			//修改时日志
			String sql = "insert into t_log_upproc (id,fd_fwno,fd_uptime,fd_dname,fd_dtype,fd_dstat,fd_msg,fd_bdsize) values("+
					"replace(uuid(),'-',''),"+
					"'"+fd_lrr+"',"+
					"'"+StaticTools.getDatetime()+"',"+
					"'',"+
					"'修改矿井信息',"+
					"'成功',"+
					"'安全监测系统',"+
					"'')";
			mineAttributeService.executeSql(sql);
			systemService.addLog(message, Globals.Log_Type_UPDATE,
					Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "矿井信息维护更新失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		String sql = "SELECT	fd_name,fd_addr,fd_pl,fd_ftpaddr,fd_ftpusr,fd_ftppw,fd_upml,fd_ftpport,fd_upmlbak,fd_username,fd_password,"
				+ " fd_dir FROM	t_fw_task a LEFT JOIN t_fw_node b ON a.pk_node = b.pk_node WHERE	fd_autostart = '是' and fd_type='安全监测系统'  and fd_name='"
				+ fd_lrr + "'";
		DataSimpDto ftpSql = new DataSimpDto(sql);
		// 在本地生产txt文件
		String cs_mine_code = mineAttribute.getCsMineCode();
		Integer cs_mine_banci = mineAttribute.getCsMineBanci();
		String cs_parent_company = mineAttribute.getCsParentCompany();
		String cs_mine_name = mineAttribute.getCsMineName();
		String cs_mine_shortname = mineAttribute.getCsMineShortname();
		String cs_economic_type = mineAttribute.getCsEconomicType();
		String cs_subrelation_code = mineAttribute.getCsSubrelationCode();
		String cs_minehead_name = mineAttribute.getCsMineheadName();
		String cs_mine_addr = mineAttribute.getCsMineAddr();
		BigDecimal cs_capacity_annual = mineAttribute.getCsCapacityAnnual();
		BigDecimal cs_capacity_real = mineAttribute.getCsCapacityReal();
		BigDecimal cs_person_num = mineAttribute.getCsPersonNum();
		BigDecimal cs_well_time = mineAttribute.getCsWellTime();
		String cs_btarea_phone = mineAttribute.getCsBtareaPhone();
		BigDecimal cs_mine_field = mineAttribute.getCsMineField();
		String cs_coal_seam = mineAttribute.getCsCoalSeam();
		String cs_minelicen_num = mineAttribute.getCsMinelicenNum();
		String cs_splicen_num = mineAttribute.getCsSplicenNum();
		String cs_prolicen_num = mineAttribute.getCsProlicenNum();
		String cs_mheadlicen_num = mineAttribute.getCsMheadlicenNum();
		String cs_mheadquali_num = mineAttribute.getCsMheadqualiNum();
		String cs_busilicen_num = mineAttribute.getCsBusilicenNum();
		String cs_gas_level = mineAttribute.getCsGasLevel();
		BigDecimal cs_gas_abs = mineAttribute.getCsGasAbs();
		BigDecimal cs_gas_com = mineAttribute.getCsGasCom();
		String cs_main_server_ip = mineAttribute.getCsMainServerIp();
		String cs_bak_server_ip = mineAttribute.getCsBakServerIp();
		String cs_bak_ip = mineAttribute.getCsBakIp();
		BigDecimal cm_y_coor = mineAttribute.getCmYCoor();
		BigDecimal cm_x_coor = mineAttribute.getCmXCoor();
		String cs_remark = mineAttribute.getCsRemark();

		SaveFile savefile = new SaveFile();
		StringBuffer sb = new StringBuffer();
		sb.append(cs_mine_code + ";");
		if (cs_mine_banci == null) {
			sb.append("" + ";");
		} else {
			sb.append(cs_mine_banci + ";");
		}
		sb.append(cs_parent_company + ";");
		sb.append(cs_mine_name + ";");
		sb.append(cs_mine_shortname + ";");
		sb.append(cs_economic_type + ";");
		/**
		 * 
		 * cs_mine_code cs_mine_banci cs_parent_company cs_mine_name
		 * cs_mine_shortname cs_economic_type cs_subrelation_code
		 * cs_minehead_name cs_mine_addr cs_capacity_annual cs_capacity_real
		 * cs_person_num cs_well_time cs_btarea_phone cs_mine_field cs_coal_seam
		 * cs_minelicen_num cs_splicen_num cs_prolicen_num cs_mheadlicen_num
		 * cs_mheadquali_num cs_busilicen_num cs_gas_level cs_gas_abs cs_gas_com
		 * cs_main_server_ip cs_bak_server_ip cs_bak_ip cm_y_coor cm_x_coor
		 * cs_remark
		 */
		sb.append(cs_subrelation_code + ";");
		sb.append(cs_minehead_name + ";");
		sb.append(cs_mine_addr + ";");
		if (cs_capacity_annual == null) {
			sb.append("" + ";");
		} else {
			sb.append(cs_capacity_annual + ";");
		}
		if (cs_capacity_real == null) {
			sb.append("" + ";");
		} else {
			sb.append(cs_capacity_real + ";");
		}
		if (cs_person_num == null) {
			sb.append("" + ";");
		} else {
			sb.append(cs_person_num + ";");
		}
		if (cs_well_time == null) {
			sb.append("" + ";");

		} else {

			sb.append(cs_well_time + ";");
		}
		sb.append(cs_btarea_phone + ";");
		if (cs_mine_field == null) {
			sb.append("" + ";");

		} else {

			sb.append(cs_mine_field + ";");
		}
		sb.append(cs_coal_seam + ";");
		sb.append(cs_minelicen_num + ";");
		sb.append(cs_splicen_num + ";");
		sb.append(cs_prolicen_num + ";");
		sb.append(cs_mheadlicen_num + ";");
		sb.append(cs_mheadquali_num + ";");
		sb.append(cs_busilicen_num + ";");
		sb.append(cs_gas_level + ";");
		if (cs_gas_abs == null) {
			sb.append("" + ";");
		} else {
			sb.append(cs_gas_abs + ";");
		}
		if (cs_gas_com == null) {
			sb.append("" + ";");
		} else {

			sb.append(cs_gas_com + ";");
		}
		sb.append(cs_main_server_ip + ";");
		sb.append(cs_bak_server_ip + ";");
		sb.append(cs_bak_ip + ";");
		if (cm_y_coor == null) {
			sb.append("" + ";");

		} else {

			sb.append(cm_y_coor + ";");
		}
		if (cm_x_coor == null) {
			sb.append("" + ";");
		} else {
			sb.append(cm_x_coor + ";");
		}
		sb.append(cs_remark + "||");
		String fd_upmlbak = ftpSql.getDataValue("fd_upmlbak", 0);
		// 获取当前时间
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
		String sj = df.format(new Date());
		StringBuffer MgName = new StringBuffer();
		MgName.append(mineAttribute.getCsMineCode());
		MgName.append("_JJ_");
		MgName.append(sj.toString());
		savefile.saveandupload(sb, "txt", fd_upmlbak, MgName.toString(), null);
		//添加生成文件日志
		 sql = "insert into t_log_upproc (id,fd_fwno,fd_uptime,fd_dname,fd_dtype,fd_dstat,fd_msg,fd_bdsize) values("+
				"replace(uuid(),'-',''),"+
				"'"+fd_lrr+"',"+
				"'"+StaticTools.getDatetime()+"',"+
				"'"+MgName.toString()+".txt"+"',"+
				"'生成本地文件',"+
				"'成功',"+
				"'安全监测系统',"+
				"'"+sb.length()+"')";
		mineAttributeService.executeSql(sql);
		// 上传ftp到服务器
				/**
				 * 上传文件操作
				 *
				 * @param hostname
				 *            ftp服务器ip
				 * @param port
				 *            ftp端口
				 * @param username
				 *            登陆名
				 * @param password
				 *            密码
				 * @param directory
				 *            ftp保存的文件夹名
				 * @param localPath
				 *            本地文件的绝对路径
				 * @param remotePath
				 *            远程路径,只需要文件名
				 */

				String fd_ftpaddr = ftpSql.getDataValue("fd_ftpaddr", 0);// ftp服务器ip
				int fd_ftpport = Integer.parseInt(ftpSql.getDataValue("fd_ftpport", 0));// ftp端口
				String fd_ftpusr = ftpSql.getDataValue("fd_ftpusr", 0);
				String fd_ftppw = ftpSql.getDataValue("fd_ftppw", 0);

				String fd_upml = ftpSql.getDataValue("fd_upml", 0);
				MgName.append(".txt");
				ContinueFTP continueFTP = new ContinueFTP();
				continueFTP.uploadFile(fd_ftpaddr, fd_ftpport, fd_ftpusr, fd_ftppw,
						fd_upml, fd_upmlbak+"/"+MgName.toString(), MgName.toString());
				//添加上传FTP日志
				sql = "insert into t_log_upproc (id,fd_fwno,fd_uptime,fd_dname,fd_dtype,fd_dstat,fd_msg,fd_bdsize) values("+
						"replace(uuid(),'-',''),"+
						"'"+fd_lrr+"',"+
						"'"+StaticTools.getDatetime()+"',"+
						"'"+MgName.toString()+"',"+
						"'FTP',"+
						"'成功',"+
						"'安全监测系统',"+
						"'"+sb.length()+"')";
				mineAttributeService.executeSql(sql);
		return j;
	}

	/**
	 * 更新矿井信息维护
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping(params = "doTUpdate")
	@ResponseBody
	public AjaxJson doTUpdate(MineAttributeEntity mineAttribute,
			HttpServletRequest request) {
		String message = null;
		AjaxJson j = new AjaxJson();
		// 获取当前登录用户
				String fd_lrr = ResourceUtil.getUserSystemData("sysUserCode");
				switch (Integer.parseInt(fd_lrr)) {
				case 18:
					fd_lrr = "1890煤矿";
					break;
				case 19:
					fd_lrr = "1930煤矿";
					break;
				case 21:
					fd_lrr = "2130煤矿";
					break;

				}
		message = "矿井信息维护更新成功";
		MineAttributeEntity t = mineAttributeService.get(
				MineAttributeEntity.class, mineAttribute.getId());
		try {
			MyBeanUtils.copyBeanNotNull2Bean(mineAttribute, t);
			mineAttributeService.saveOrUpdate(t);
			//修改时日志
			String sql = "insert into t_log_upproc (id,fd_fwno,fd_uptime,fd_dname,fd_dtype,fd_dstat,fd_msg,fd_bdsize) values("+
					"replace(uuid(),'-',''),"+
					"'"+fd_lrr+"',"+
					"'"+StaticTools.getDatetime()+"',"+
					"'',"+
					"'修改矿井信息',"+
					"'成功',"+
					"'安全监测系统',"+
					"'')";
			mineAttributeService.executeSql(sql);
			systemService.addLog(message, Globals.Log_Type_UPDATE,
					Globals.Log_Leavel_INFO);
		} catch (Exception e) {
			e.printStackTrace();
			message = "矿井信息维护更新失败";
			throw new BusinessException(e.getMessage());
		}
		j.setMsg(message);
		
		String sql = "SELECT	fd_name,fd_addr,fd_pl,fd_ftpaddr,fd_ftpusr,fd_ftppw,fd_upml,fd_ftpport,fd_upmlbak,fd_username,fd_password,"
				+ " fd_dir FROM	t_fw_task a LEFT JOIN t_fw_node b ON a.pk_node = b.pk_node WHERE	fd_autostart = '是' and fd_type='安全监测系统' and fd_name='"
				+ fd_lrr + "'";
		DataSimpDto ftpSql = new DataSimpDto(sql);
		// 在本地生产txt文件
		String cs_mine_code = mineAttribute.getCsMineCode();
		Integer cs_mine_banci = mineAttribute.getCsMineBanci();
		String cs_parent_company = mineAttribute.getCsParentCompany();
		String cs_mine_name = mineAttribute.getCsMineName();
		String cs_mine_shortname = mineAttribute.getCsMineShortname();
		String cs_economic_type = mineAttribute.getCsEconomicType();
		String cs_subrelation_code = mineAttribute.getCsSubrelationCode();
		String cs_minehead_name = mineAttribute.getCsMineheadName();
		String cs_mine_addr = mineAttribute.getCsMineAddr();
		BigDecimal cs_capacity_annual = mineAttribute.getCsCapacityAnnual();
		BigDecimal cs_capacity_real = mineAttribute.getCsCapacityReal();
		BigDecimal cs_person_num = mineAttribute.getCsPersonNum();
		BigDecimal cs_well_time = mineAttribute.getCsWellTime();
		String cs_btarea_phone = mineAttribute.getCsBtareaPhone();
		BigDecimal cs_mine_field = mineAttribute.getCsMineField();
		String cs_coal_seam = mineAttribute.getCsCoalSeam();
		String cs_minelicen_num = mineAttribute.getCsMinelicenNum();
		String cs_splicen_num = mineAttribute.getCsSplicenNum();
		String cs_prolicen_num = mineAttribute.getCsProlicenNum();
		String cs_mheadlicen_num = mineAttribute.getCsMheadlicenNum();
		String cs_mheadquali_num = mineAttribute.getCsMheadqualiNum();
		String cs_busilicen_num = mineAttribute.getCsBusilicenNum();
		String cs_gas_level = mineAttribute.getCsGasLevel();
		BigDecimal cs_gas_abs = mineAttribute.getCsGasAbs();
		BigDecimal cs_gas_com = mineAttribute.getCsGasCom();
		String cs_main_server_ip = mineAttribute.getCsMainServerIp();
		String cs_bak_server_ip = mineAttribute.getCsBakServerIp();
		String cs_bak_ip = mineAttribute.getCsBakIp();
		BigDecimal cm_y_coor = mineAttribute.getCmYCoor();
		BigDecimal cm_x_coor = mineAttribute.getCmXCoor();
		String cs_remark = mineAttribute.getCsRemark();

		SaveFile savefile = new SaveFile();
		StringBuffer sb = new StringBuffer();
		sb.append(cs_mine_code + ";");
		if (cs_mine_banci == null) {
			sb.append("" + ";");
		} else {
			sb.append(cs_mine_banci + ";");
		}
		sb.append(cs_parent_company + ";");
		sb.append(cs_mine_name + ";");
		sb.append(cs_mine_shortname + ";");
		sb.append(cs_economic_type + ";");
		/**
		 * 
		 * cs_mine_code cs_mine_banci cs_parent_company cs_mine_name
		 * cs_mine_shortname cs_economic_type cs_subrelation_code
		 * cs_minehead_name cs_mine_addr cs_capacity_annual cs_capacity_real
		 * cs_person_num cs_well_time cs_btarea_phone cs_mine_field cs_coal_seam
		 * cs_minelicen_num cs_splicen_num cs_prolicen_num cs_mheadlicen_num
		 * cs_mheadquali_num cs_busilicen_num cs_gas_level cs_gas_abs cs_gas_com
		 * cs_main_server_ip cs_bak_server_ip cs_bak_ip cm_y_coor cm_x_coor
		 * cs_remark
		 */
		sb.append(cs_subrelation_code + ";");
		sb.append(cs_minehead_name + ";");
		sb.append(cs_mine_addr + ";");
		if (cs_capacity_annual == null) {
			sb.append("" + ";");
		} else {
			sb.append(cs_capacity_annual + ";");
		}
		if (cs_capacity_real == null) {
			sb.append("" + ";");
		} else {
			sb.append(cs_capacity_real + ";");
		}
		if (cs_person_num == null) {
			sb.append("" + ";");
		} else {
			sb.append(cs_person_num + ";");
		}
		if (cs_well_time == null) {
			sb.append("" + ";");

		} else {

			sb.append(cs_well_time + ";");
		}
		sb.append(cs_btarea_phone + ";");
		if (cs_mine_field == null) {
			sb.append("" + ";");

		} else {

			sb.append(cs_mine_field + ";");
		}
		sb.append(cs_coal_seam + ";");
		sb.append(cs_minelicen_num + ";");
		sb.append(cs_splicen_num + ";");
		sb.append(cs_prolicen_num + ";");
		sb.append(cs_mheadlicen_num + ";");
		sb.append(cs_mheadquali_num + ";");
		sb.append(cs_busilicen_num + ";");
		sb.append(cs_gas_level + ";");
		if (cs_gas_abs == null) {
			sb.append("" + ";");
		} else {
			sb.append(cs_gas_abs + ";");
		}
		if (cs_gas_com == null) {
			sb.append("" + ";");
		} else {

			sb.append(cs_gas_com + ";");
		}
		sb.append(cs_main_server_ip + ";");
		sb.append(cs_bak_server_ip + ";");
		sb.append(cs_bak_ip + ";");
		if (cm_y_coor == null) {
			sb.append("" + ";");

		} else {

			sb.append(cm_y_coor + ";");
		}
		if (cm_x_coor == null) {
			sb.append("" + ";");
		} else {
			sb.append(cm_x_coor + ";");
		}
		sb.append(cs_remark + "||");
		String fd_upmlbak = ftpSql.getDataValue("fd_upmlbak", 0);
		// 获取当前时间
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");// 设置日期格式
		String sj = df.format(new Date());
		StringBuffer MgName = new StringBuffer();
		MgName.append(mineAttribute.getCsMineCode());
		MgName.append("_JJ_");
		MgName.append(sj.toString());
		savefile.saveandupload(sb, "txt", fd_upmlbak, MgName.toString(), null);
		//添加生成文件日志
		 sql = "insert into t_log_upproc (id,fd_fwno,fd_uptime,fd_dname,fd_dtype,fd_dstat,fd_msg,fd_bdsize) values("+
				"replace(uuid(),'-',''),"+
				"'"+fd_lrr+"',"+
				"'"+StaticTools.getDatetime()+"',"+
				"'"+MgName.toString()+".txt"+"',"+
				"'生成本地文件',"+
				"'成功',"+
				"'安全监测系统',"+
				"'"+sb.length()+"')";
		mineAttributeService.executeSql(sql);
		// 上传ftp到服务器
		/**
		 * 上传文件操作
		 *
		 * @param hostname
		 *            ftp服务器ip
		 * @param port
		 *            ftp端口
		 * @param username
		 *            登陆名
		 * @param password
		 *            密码
		 * @param directory
		 *            ftp保存的文件夹名
		 * @param localPath
		 *            本地文件的绝对路径
		 * @param remotePath
		 *            远程路径,只需要文件名
		 */

		String fd_ftpaddr = ftpSql.getDataValue("fd_ftpaddr", 0);// ftp服务器ip
		int fd_ftpport = Integer.parseInt(ftpSql.getDataValue("fd_ftpport", 0));// ftp端口
		String fd_ftpusr = ftpSql.getDataValue("fd_ftpusr", 0);
		String fd_ftppw = ftpSql.getDataValue("fd_ftppw", 0);

		String fd_upml = ftpSql.getDataValue("fd_upml", 0);
		MgName.append(".txt");
		ContinueFTP continueFTP = new ContinueFTP();
		continueFTP.uploadFile(fd_ftpaddr, fd_ftpport, fd_ftpusr, fd_ftppw,
				fd_upml, fd_upmlbak+"/"+MgName.toString(), MgName.toString());
		//添加上传FTP日志
		sql = "insert into t_log_upproc (id,fd_fwno,fd_uptime,fd_dname,fd_dtype,fd_dstat,fd_msg,fd_bdsize) values("+
				"replace(uuid(),'-',''),"+
				"'"+fd_lrr+"',"+
				"'"+StaticTools.getDatetime()+"',"+
				"'"+MgName.toString()+"',"+
				"'FTP',"+
				"'成功',"+
				"'安全监测系统',"+
				"'"+sb.length()+"')";
		mineAttributeService.executeSql(sql);

		return j;
	}

	/**
	 * 矿井信息维护新增页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goAdd")
	public ModelAndView goAdd(MineAttributeEntity mineAttribute,
			HttpServletRequest req) {
		if (StringUtil.isNotEmpty(mineAttribute.getId())) {
			mineAttribute = mineAttributeService.getEntity(
					MineAttributeEntity.class, mineAttribute.getId());
			req.setAttribute("mineAttributePage", mineAttribute);
		}
		return new ModelAndView("com/jeecg/mineattribute/mineAttribute-add");
	}

	/**
	 * 矿井信息维护编辑页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goUpdate")
	public ModelAndView goUpdate(MineAttributeEntity mineAttribute,
			HttpServletRequest req) {
		if (StringUtil.isNotEmpty(mineAttribute.getId())) {
			mineAttribute = mineAttributeService.getEntity(
					MineAttributeEntity.class, mineAttribute.getId());
			req.setAttribute("mineAttributePage", mineAttribute);
		}
		if (ResourceUtil.getUserSystemData("sysUserCode").equals("admin")) {
			return new ModelAndView(
					"com/jeecg/mineattribute/mineAttribute-update");

		} else {
			return new ModelAndView(
					"com/jeecg/mineattribute/mineAttribute-Tupdate");
		}
	}

	/**
	 * 矿井信息维护编辑页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "goTUpdate")
	public ModelAndView goTUpdate(MineAttributeEntity mineAttribute,
			HttpServletRequest req) {
		// 获取当前登录用户
		String fd_lrr = ResourceUtil.getUserSystemData("sysUserCode");
		switch (Integer.parseInt(fd_lrr)) {
		case 18:
			fd_lrr = "1890煤矿";
			break;
		case 19:
			fd_lrr = "1930煤矿";
			break;
		case 21:
			fd_lrr = "2130煤矿";
			break;

		}
		String sql = "select id from mine_attribute where mine_type='" + fd_lrr
				+ "'";
		DataSimpDto getId = new DataSimpDto(sql);
		String id = getId.getDataValue("id", 0);
		if (StringUtil.isNotEmpty(id)) {
			mineAttribute = mineAttributeService.getEntity(
					MineAttributeEntity.class, id);
			req.setAttribute("mineAttributePage", mineAttribute);
		}
		return new ModelAndView("com/jeecg/mineattribute/mineAttribute-Tupdate");
	}

	/**
	 * 导入功能跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "upload")
	public ModelAndView upload(HttpServletRequest req) {
		req.setAttribute("controller_name", "mineAttributeController");
		return new ModelAndView("common/upload/pub_excel_upload");
	}

	/**
	 * 导出excel
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(params = "exportXls")
	public String exportXls(MineAttributeEntity mineAttribute,
			HttpServletRequest request, HttpServletResponse response,
			DataGrid dataGrid, ModelMap modelMap) {
		CriteriaQuery cq = new CriteriaQuery(MineAttributeEntity.class,
				dataGrid);
		org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq,
				mineAttribute, request.getParameterMap());
		List<MineAttributeEntity> mineAttributes = this.mineAttributeService
				.getListByCriteriaQuery(cq, false);
		modelMap.put(NormalExcelConstants.FILE_NAME, "矿井信息维护");
		modelMap.put(NormalExcelConstants.CLASS, MineAttributeEntity.class);
		modelMap.put(NormalExcelConstants.PARAMS, new ExportParams("矿井信息维护列表",
				"导出人:" + ResourceUtil.getSessionUserName().getRealName(),
				"导出信息"));
		modelMap.put(NormalExcelConstants.DATA_LIST, mineAttributes);
		return NormalExcelConstants.JEECG_EXCEL_VIEW;
	}

	/**
	 * 导出excel 使模板
	 * 
	 * @param request
	 * @param response
	 */
	@RequestMapping(params = "exportXlsByT")
	public String exportXlsByT(MineAttributeEntity mineAttribute,
			HttpServletRequest request, HttpServletResponse response,
			DataGrid dataGrid, ModelMap modelMap) {
		modelMap.put(NormalExcelConstants.FILE_NAME, "矿井信息维护");
		modelMap.put(NormalExcelConstants.CLASS, MineAttributeEntity.class);
		modelMap.put(NormalExcelConstants.PARAMS, new ExportParams("矿井信息维护列表",
				"导出人:" + ResourceUtil.getSessionUserName().getRealName(),
				"导出信息"));
		modelMap.put(NormalExcelConstants.DATA_LIST, new ArrayList());
		return NormalExcelConstants.JEECG_EXCEL_VIEW;
	}

	@SuppressWarnings("unchecked")
	@RequestMapping(params = "importExcel", method = RequestMethod.POST)
	@ResponseBody
	public AjaxJson importExcel(HttpServletRequest request,
			HttpServletResponse response) {
		AjaxJson j = new AjaxJson();

		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
		for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
			MultipartFile file = entity.getValue();// 获取上传文件对象
			ImportParams params = new ImportParams();
			params.setTitleRows(2);
			params.setHeadRows(1);
			params.setNeedSave(true);
			try {
				List<MineAttributeEntity> listMineAttributeEntitys = ExcelImportUtil
						.importExcel(file.getInputStream(),
								MineAttributeEntity.class, params);
				for (MineAttributeEntity mineAttribute : listMineAttributeEntitys) {
					mineAttributeService.save(mineAttribute);
				}
				j.setMsg("文件导入成功！");
			} catch (Exception e) {
				j.setMsg("文件导入失败！");
				logger.error(ExceptionUtil.getExceptionMessage(e));
			} finally {
				try {
					file.getInputStream().close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return j;
	}

	@RequestMapping(method = RequestMethod.GET)
	@ResponseBody
	public List<MineAttributeEntity> list() {
		List<MineAttributeEntity> listMineAttributes = mineAttributeService
				.getList(MineAttributeEntity.class);
		return listMineAttributes;
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.GET)
	@ResponseBody
	public ResponseEntity<?> get(@PathVariable("id") String id) {
		MineAttributeEntity task = mineAttributeService.get(
				MineAttributeEntity.class, id);
		if (task == null) {
			return new ResponseEntity(HttpStatus.NOT_FOUND);
		}
		return new ResponseEntity(task, HttpStatus.OK);
	}

	@RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
	@ResponseBody
	public ResponseEntity<?> create(
			@RequestBody MineAttributeEntity mineAttribute,
			UriComponentsBuilder uriBuilder) {
		// 调用JSR303 Bean Validator进行校验，如果出错返回含400错误码及json格式的错误信息.
		Set<ConstraintViolation<MineAttributeEntity>> failures = validator
				.validate(mineAttribute);
		if (!failures.isEmpty()) {
			return new ResponseEntity(
					BeanValidators.extractPropertyAndMessage(failures),
					HttpStatus.BAD_REQUEST);
		}

		// 保存
		try {
			mineAttributeService.save(mineAttribute);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResponseEntity(HttpStatus.NO_CONTENT);
		}
		// 按照Restful风格约定，创建指向新任务的url, 也可以直接返回id或对象.
		String id = mineAttribute.getId();
		URI uri = uriBuilder.path("/rest/mineAttributeController/" + id)
				.build().toUri();
		HttpHeaders headers = new HttpHeaders();
		headers.setLocation(uri);

		return new ResponseEntity(headers, HttpStatus.CREATED);
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
	public ResponseEntity<?> update(
			@RequestBody MineAttributeEntity mineAttribute) {
		// 调用JSR303 Bean Validator进行校验，如果出错返回含400错误码及json格式的错误信息.
		Set<ConstraintViolation<MineAttributeEntity>> failures = validator
				.validate(mineAttribute);
		if (!failures.isEmpty()) {
			return new ResponseEntity(
					BeanValidators.extractPropertyAndMessage(failures),
					HttpStatus.BAD_REQUEST);
		}

		// 保存
		try {
			mineAttributeService.saveOrUpdate(mineAttribute);
		} catch (Exception e) {
			e.printStackTrace();
			return new ResponseEntity(HttpStatus.NO_CONTENT);
		}

		// 按Restful约定，返回204状态码, 无内容. 也可以返回200状态码.
		return new ResponseEntity(HttpStatus.NO_CONTENT);
	}

	@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
	@ResponseStatus(HttpStatus.NO_CONTENT)
	public void delete(@PathVariable("id") String id) {
		mineAttributeService.deleteEntityById(MineAttributeEntity.class, id);
	}
}
